package com.hl.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.entity.SysIntegral;
import com.hl.sfzy.entity.SysUser;
import com.hl.sfzy.mapper.SysIntegralMapper;
import com.hl.sfzy.mapper.SysUserMapper;
import com.hl.sfzy.service.SysUserService;
import com.hl.sfzy.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: sfzy
 * @ClassName SysUserServiceImpl
 * @description:
 * @author: lyp
 * @create: 2024-07-10 14:14
 * @Version 1.0
 **/
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysIntegralMapper sysIntegralMapper;

    //事务
    @Transactional
    @Override
    public int add(SysUser entity) {
//        给密码加密
        String encode = MD5Util.encode(entity.getUserpwd());
        entity.setUserpwd(encode);

//        当注册的时候也给积分表默认插入一个字段并且给10积分
        // 先保存用户信息
        int insert = sysUserMapper.insert(entity);
        // 获取用户ID，用于关联积分表
        Integer userId = entity.getId();
        // 创建并保存积分信息
        SysIntegral sysIntegral = new SysIntegral();
        sysIntegral.setUserId(userId);
        sysIntegral.setIntegral(10); // 初始积分为10
        sysIntegralMapper.insert(sysIntegral);
        return insert;
    }

    @Override
    public int edit(SysUser entity) {
        if(entity.getUserpwd()!=null && entity.getUserpwd()!=""){
            String encode = MD5Util.encode(entity.getUserpwd());
            entity.setUserpwd(encode);
        }
        return sysUserMapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        return sysUserMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return sysUserMapper.deleteBatch(ids);
    }

    @Override
    public List<SysUser> find(SysUser entity) {
        String encode = MD5Util.encode(entity.getUserpwd());
        entity.setUserpwd(encode);
        return sysUserMapper.select(entity);
    }

    @Override
    public SysUser findById(Integer id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, SysUser entity) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //当前页数据
        List<SysUser> list = sysUserMapper.select(entity);
        //分页信息对象
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();

        //map存储当前页数据、总页数、总记录数
        map.put(CommonConstant.PAGE_LIST,list);
        map.put(CommonConstant.PAGE_TOTAL,pageInfo.getPages());
        map.put(CommonConstant.TOTAL,pageInfo.getTotal());

        return map;
    }

    @Override
    public SysUser findByUsername(String username) {
        return sysUserMapper.selectByUsername(username);
    }
}

